var fs = require('fs');
var path = require('path');
var multer = require('multer');
var uuid = require('uuid');

var store = multer.diskStorage({
    destination: function (req, res, cb) {
        var dir = path.join(__dirname, '../../public/pictures');
        if (fs.existsSync(dir)) {
            var state = fs.statSync(dir);
            if (!state.isDirectory()) {
                fs.mkdirSync(dir);
            }
        } else {
            fs.mkdirSync(dir);
        }
        cb(null,dir)
    },
    filename: function (req, file, cb) {
        var id = uuid.v1().toString();
        cb(null,id +"."+ file.originalname.split('.')[1]);
    }
})

module.exports = multer({ storage: store })



